<?php

namespace App\Http\Controllers\staff;

use Illuminate\Http\Request;
use DB;
use Cookie;
use Hash;
use App\Http\Controllers\Controller;
//模型
use App\Models\Role;

class AccountController extends Controller
{
    public function getIndex(Request $request){
        $staffInfo = $request -> cookie('staffInfo');
        //获取账号列表
        if($staffInfo['staff_role_id'] == 1){
            //查询管理区域
            $areaArr = DB::table('area_staff as as')
                -> leftJoin('area as a','a.id','=','as.area_id')
                -> where('staff_id',$staffInfo['id'])
                -> get();
            foreach($areaArr as $k=>$v){
                $areaArr[$k]['staffArr'] = DB::table('area_staff as as')
                    -> leftJoin('staff as s','s.id','=','as.staff_id')
                    -> where('as.area_id',$v['area_id'])
                    -> where('s.staff_role_id',2)
                    -> get();
            }
        }else{
            $areaArr = [];
        }
        return view('staff.account.index',['areaArr'=>$areaArr]);
    }

    public function getCreate(Request $request){
        //查询员工角色
        $role = DB::table('staff_role') -> get();
        $staff_id = $request -> cookie('staffInfo')['id'];
        //查询员工所负责区域
        $areaArr = DB::table('area_staff') -> where('staff_id',$staff_id) -> select('area_id') -> get();
        $areaInfo = DB::table('area') -> whereIn('id',$areaArr) -> get();
        return view('staff.account.create',['role'=>$role,'areaInfo'=>$areaInfo]);
    }

    public function postStore(Request $request){
        $info = $request -> except('_token','area_id');
        $info['pwd'] = Hash::make($info['pwd']);
        //插入仓库表
        $staff_id = DB::table('staff') -> insertGetId($info);
        //插入区域员工关系表
        $res = DB::table('area_staff') -> insert(['area_id'=>$request -> input('area_id'),'staff_id'=>$staff_id]);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }

    public function getEdit(Request $request){
        $id = $request -> input('id');
        //查询仓库表
        $data = DB::table('staff')
            -> where('id',$id)
            -> first();
        $data['area_id'] = $request -> input('area_id');
        //查询员工角色
        $role = DB::table('staff_role') -> get();
        $staff_id = $request -> cookie('staffInfo')['id'];
        //查询员工所负责区域
        $areaArr = DB::table('area_staff') -> where('staff_id',$staff_id) -> select('area_id') -> get();
        $areaInfo = DB::table('area') -> whereIn('id',$areaArr) -> get();
        return view('staff.account.edit',['data'=> $data,'role'=>$role,'areaInfo'=>$areaInfo]);
    }

    public function postUpdate(Request $request){
        $id = $request -> input('id');
        $info = $request -> except('_token','id','area_id','areas');
        $areas = $request -> input('areas');
        $area_id = $request -> input('area_id');
        //修改仓库表
        DB::table('staff') -> where('id',$id) -> update($info);
        //编辑区域员工关系表
        $res = DB::table('area_staff') -> where(['area_id'=>$areas,'staff_id'=>$id]) ->update(['area_id'=>$area_id]);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return json_encode($data);
    }

    public function postDestroy(Request $request){
        $id = $request -> input('id');
        //查询区域员工关系表
        $count = DB::table('area_staff') -> where('staff_id',$id) -> count();
        if(!$count){
            //删除员工表数据
            $res = DB::table('staff') -> delete($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = 0;
            }
        }else{
            $data['status'] = -1;
        }
        return json_encode($data);
    }
}